package pers.fjl.test.controller;

import pers.fjl.common.constant.MessageConstant;
import pers.fjl.common.entity.QueryPageBean;
import pers.fjl.common.entity.Result;
import pers.fjl.common.validation.ValidGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import pers.fjl.test.annotation.LoginRequired;
import pers.fjl.test.service.UserItemService;
import pers.fjl.common.po.User;

import javax.annotation.Resource;

/**
 * 用户模块
 *
 * @author fangjiale 2020年11月26日
 */

@Api(value = "用户模块", description = "用户模块的接口信息")
@RequestMapping("/user")
@RestController
@CrossOrigin

public class UserItemController {
    @Resource
    private UserItemService userItemService;

    @ApiOperation(value = "添加用户", notes = "提交表单，添加用户")
    @PostMapping("/add")
    public Result add(@RequestBody @Validated({ValidGroup.GroupA.class}) User user) {
        boolean flag = userItemService.addUser(user);
        return new Result(flag, MessageConstant.OK);
    }

    @LoginRequired
    @ApiOperation(value = "分页查询", notes = "返回分页数据")
    @PostMapping("/findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        return new Result(true, MessageConstant.OK, "获取分页数据成功", userItemService.findPage(queryPageBean));
    }

    @ApiOperation(value = "禁用用户", notes = "禁用用户")
    @GetMapping("/forbid")
    public Result forbid(String id) {
        userItemService.forbid(id);
        return new Result(true, MessageConstant.OK);
    }

    @ApiOperation(value = "删除用户", notes = "删除用户")
    @DeleteMapping("/deleteById")
    public Result deleteById(String id) {
        boolean flag = userItemService.deleteById(id);
        return new Result(flag, MessageConstant.OK);
    }

    @ApiOperation(value = "查找用户", notes = "根据用户id查找用户")
    @DeleteMapping("/findById")
    public User findById(String id) {
        return userItemService.findById(id);
    }

    @ApiOperation(value = "编辑用户", notes = "编辑用户")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody @Validated({ValidGroup.GroupB.class}) User user) {
        boolean flag = userItemService.updateUser(user);
        return new Result(flag, MessageConstant.OK);
    }

    @ApiOperation(value = "用户状态更新", notes = "更新用户的禁用状态")
    @PutMapping("/{id}/state/{dataStatus}")
    public Result userStateChanged(@PathVariable("id") String id,
                                   @PathVariable("dataStatus") boolean dataStatus) {
        boolean flag = userItemService.userStateChanged(id, dataStatus);
        return new Result(flag, MessageConstant.OK);
    }
}
